'''
@Project ：pythonApiTest 
@File    ：SwaggerParser.py
@IDE     ：PyCharm 
@Author  ：胡文
@Date    ：2025/2/28 上午9:31 
'''

import requests
from openapi_spec_validator import validate_spec


class SwaggerParser:
    def __init__(self, swagger_url):
        self.swagger_url = swagger_url

    def get_apis(self):
        """获取接口清单"""
        response = requests.get(self.swagger_url)
        spec_dict = response.json()
        validate_spec(spec_dict)  # 验证OpenAPI规范

        apis = []
        for path, methods in spec_dict['paths'].items():
            for method, details in methods.items():
                apis.append({
                    'path': path,
                    'method': method.upper(),
                    'parameters': details.get('parameters', []),
                    'responses': details['responses']
                })
        return apis